package tank

import (
	"go-study-history/tank_war/clinet"
	"go-study-history/tank_war/entity"
	"go-study-history/tank_war/message"
	"golang.org/x/net/websocket"
	"log"
)

/**
接收消息
*/
func OnMessage() {
	for {
		ws := clinet.WsClient.Client
		if ws == nil {
			ws = clinet.Connect("接收消息未创建客户端,启动连接")
			//登录上线
			Login()
		}
		var temp []entity.ReceiveBody
		er := websocket.JSON.Receive(ws, &temp)
		if er != nil {
			clinet.WsClient.Client = nil
			log.Printf("接收消息中监听断线，客户端初始化为nil,\n错误内容:%s\n", er)
		}
		responseResult(temp)
	}
}

func responseResult(body []entity.ReceiveBody) {
	//var me entity.ReceiveBody
	var enemy []entity.ReceiveBody
	for _, item := range body {
		switch item.Type {
		//地图
		case "map":
			log.Printf("地图数据：%v", item)
			entity.SetMap(item.Width, item.Height)
			//坦克
		case "tank":
			//是敌人
			if item.IsEnemy {
				enemy = append(enemy, item)
				//是自己
			} else {
				//me = item
			}
			//炮弹
		case "bullet":
			log.Printf("炮弹:%v", item.Direct)
			enemy = append(enemy, item)
		default:
			log.Printf("未定义数据类型,type:%s,item:%v", item.Type, item)
		}
	}

}

//登录
func Login() {
	r := entity.GetRegister("zhangweixing", "zhangweixing", "#FF7F00")
	message.SendMessage(&r)
	message.SendMessage(entity.GetAccSpeed(4))

}
